package com.key.service.impl;

import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.dto.WithdrawalAssetsDTO;
import com.key.common.dto.WithdrawalDTO;
import com.key.common.vo.AssetsTVO;
import com.key.entity.*;
import com.key.mapper.*;
import com.key.service.WithdrawalAssetsService;
import com.key.utils.OddNumbersUtils;
import com.key.utils.TranException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wen
 * @create 2023-02-20 10:11
 */
@Service
public class WithdrawalAssetsServiceImpl implements WithdrawalAssetsService{


    @Autowired
    ReceiptAssetsMapper receiptAssetsMapper;
    @Autowired
    WithdrawalAssetsMapper withdrawalAssetsMapper;
    @Autowired
    AssetsMapper assetsMapper;

    @Autowired
    ApprovalMapper approvalMapper;

    @Autowired
    IndexMapper indexMapper;

    @Autowired
    IndexAssetsMapper indexAssetsMapper;

    @Override
    @Transactional
    public R creatWithdrawal(WithdrawalAssetsDTO withdrawalAssetsDTO) {
        R r=new R();
        String withdrawalNumber=OddNumbersUtils.creatON("TK");
        WithdrawalAssets withdrawalAssets=withdrawalAssetsDTO.getWithdrawalAssets();

        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        String s = String.valueOf(subject.getPrincipal());
        if(s==null||s==""){
            s="张三";
        }
        withdrawalAssets.setWithdrawaler(s);

//        try {
            //将退库单与资产相关联
            withdrawalAssetsMapper.updateWithdrawalAndAssets(withdrawalNumber,withdrawalAssetsDTO.getReceiptNumber());
            withdrawalAssets.setState("发起");
            withdrawalAssets.setWithdrawalNumber(withdrawalNumber);
            withdrawalAssets.setApproveProgress("未审批");
            withdrawalAssets.setHandlers("未实现“creatWithdrawal”");
            int result =withdrawalAssetsMapper.insertWithdrawal(withdrawalAssets);

            //退库审批单添加
            SysUser sysUser= indexMapper.selectUserInfoByUserName(s);
            Company company = indexAssetsMapper.selectAssetsInCompany(sysUser.getUserId());
            Department department = indexAssetsMapper.selectDeptInfoByEpmId(sysUser.getUserId());
            Approval approval = new Approval();
            approval.setState("未审批");
            approval.setApprovalNo(withdrawalNumber);
            approval.setApplicationType("退库");
            approval.setApplicationTime(OddNumbersUtils.getDay());
            approval.setApplicationPerson(s);
            approval.setApplicationCompany(company.getCompanyName());
            approval.setApplicationDepartment(department.getDepartmentName());

            approvalMapper.insertApproval(approval);

            if(result!=0){
                r.setCode(200);
                r.setMsg("提交申请成功");
            }else {
                r.setCode(300);
                r.setMsg("生成清单失败");
            }
//        }catch (Exception e){
//            throw new TranException("事务内出错");
//        }
        r.setCode(500);
        r.setMsg("后台错误");

        return r;
    }

    @Override
    public R deleteWithdrawalAssets(String withdrawalNumber) {
        R r=new R();
        try {
            int i=withdrawalAssetsMapper.deleteWithdrawalAssetsByWithdrawalNumber(withdrawalNumber);
            int j=withdrawalAssetsMapper.deleteWithdrawalAndAssets(withdrawalNumber);
            if(i!=0&&j!=0){
                r.setCode(200);
                r.setMsg("删除成功");
            }else {
                r.setCode(300);
                r.setMsg("删除失败");
            }
        }catch (Exception e){
            throw new TranException("事务错误");
        }
        return r;
    }

    @Override
    public R editWithdrawalAssets(WithdrawalDTO withdrawalDTO) {
        R r=new R();
        int i=withdrawalAssetsMapper.updateWithdrawalAssets(withdrawalDTO);
        if(i!=0){
            r.setCode(200);
            r.setMsg("修改成功！");
        }else {
            r.setCode(300);
            r.setMsg("修改失败！");
        }
        return r;
    }


    @Override
    public R findWithdrawalAssets(WithdrawalDTO withdrawalDTO) {
        int currentPage=withdrawalDTO.getCurrentPage();
        int pageSize=withdrawalDTO.getPageSize();
        R r=new R();

        //未分页查询，用于count获取
        List<WithdrawalAssets> withdrawalAssetsList2 = withdrawalAssetsMapper.selectWithdrawal(withdrawalDTO);

        PageHelper.startPage(currentPage,pageSize);
        List<WithdrawalAssets> withdrawalAssetsList = withdrawalAssetsMapper.selectWithdrawal(withdrawalDTO);
        if(withdrawalAssetsList!=null){
            r.setCode(200);
            r.setCount(withdrawalAssetsList2.size());
            r.setMsg("success");
            r.setData(withdrawalAssetsList);
        }else {
            r.setCode(400);
            r.setMsg("查询失败！");
        }
        return r;
    }

    @Override
    public List<Assets> findAssetsByWithdrawalAssets(WithdrawalAssets withdrawalAssets) {
        List<Assets> resultList=withdrawalAssetsMapper.selectAssetsByWithdrawalAssetsNumber(withdrawalAssets.getWithdrawalNumber());
        return resultList;
    }
    @Override
    public List<AssetsTVO> findAssetsVTOByWithdrawalAssets(WithdrawalAssets withdrawalAssets) {
        List<AssetsTVO> resultList=withdrawalAssetsMapper.selectAssetsVTOByWithdrawalAssetsNumber(withdrawalAssets.getWithdrawalNumber());
        return resultList;
    }

}
